package cn.com.nr.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import cn.com.common.constants.Constants;
import cn.com.common.result.DataResult;
import cn.com.gas.entity.GasStationinfoEntity;
import cn.com.gas.service.GasStationinfoService;
import cn.com.nr.entity.NrConcreteEndangerEntity;
import cn.com.nr.entity.NrDiggingsEndangerEntity;
import cn.com.nr.entity.NrStationinfoForecastMonthEntity;
import cn.com.nr.service.NrConcreteEndangerService;
import cn.com.nr.service.NrDiggingsEndangerService;
import cn.com.nr.service.NrStationinfoForecastMonthService;
import cn.com.system.entity.SysDept;
import cn.com.system.service.DeptService;
import cn.com.system.service.impl.HttpSessionService;
import cn.com.util.TsBqUtils;



/**
 * 预估列表(月)
 *
 * @author shaochangyang
 * @email l2l84o7o5@live.com
 * @date 2025-04-30 16:29:06
 */
@Controller
@RequestMapping("/nr/stationforecastmonth")
public class NrStationinfoForecastMonthController {
   
	@Autowired
	private DeptService deptService;
	@Autowired
	private GasStationinfoService stationinfoService;
	@Autowired
	private HttpSessionService httpSessionService;
	@Autowired
    private NrStationinfoForecastMonthService nrStationinfoForecastMonthService;
	@Autowired
    private NrDiggingsEndangerService nrDiggingsEndangerService;
    @Autowired
    private NrConcreteEndangerService nrConcreteEndangerService;
    
    
    
    
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@ApiOperation(value = "查询分页数据")
	@PostMapping("/list")
	@ResponseBody
	public DataResult list(@RequestBody NrStationinfoForecastMonthEntity nrStationinfoForecastMonth) {
		String stationinfoName = nrStationinfoForecastMonth.getStationinfoName();
		String industry = nrStationinfoForecastMonth.getIndustry();
		
		List<String> stationinfoList = new ArrayList<String>();
		if(TsBqUtils.isNotNull(stationinfoName)){

			if(industry.equals("混凝土")||industry.equals("水泥制造")){
				LambdaQueryWrapper<NrConcreteEndangerEntity> queryWrapperConcrete = Wrappers.lambdaQuery();
				
				queryWrapperConcrete.eq(NrConcreteEndangerEntity::getIndustry, industry);
				
				queryWrapperConcrete.like(NrConcreteEndangerEntity::getName, stationinfoName);

				List<NrConcreteEndangerEntity> listEntity = nrConcreteEndangerService.list(queryWrapperConcrete);
				
				for (NrConcreteEndangerEntity nrConcreteEndangerEntity : listEntity) {
					stationinfoList.add(nrConcreteEndangerEntity.getId());
				}
				
			}else if(industry.equals("铁矿选矿")||industry.equals("铁矿采矿")){
				LambdaQueryWrapper<NrDiggingsEndangerEntity> queryWrapperDiggings = Wrappers.lambdaQuery();
				
				queryWrapperDiggings.eq(NrDiggingsEndangerEntity::getIndustry, industry);
				queryWrapperDiggings.like(NrDiggingsEndangerEntity::getName, stationinfoName);

				List<NrDiggingsEndangerEntity> listEntity = nrDiggingsEndangerService.list(queryWrapperDiggings);
				
				for (NrDiggingsEndangerEntity nrDiggingsEndangerEntity : listEntity) {
					stationinfoList.add(nrDiggingsEndangerEntity.getId());
				}
			}else{
				LambdaQueryWrapper<NrConcreteEndangerEntity> queryWrapperConcrete = Wrappers.lambdaQuery();
				queryWrapperConcrete.like(NrConcreteEndangerEntity::getName, stationinfoName);
				List<NrConcreteEndangerEntity> listEntity1 = nrConcreteEndangerService.list(queryWrapperConcrete);
				for (NrConcreteEndangerEntity nrConcreteEndangerEntity : listEntity1) {
					stationinfoList.add(nrConcreteEndangerEntity.getId());
				}
				
				
				LambdaQueryWrapper<NrDiggingsEndangerEntity> queryWrapperDiggings = Wrappers.lambdaQuery();
				queryWrapperDiggings.like(NrDiggingsEndangerEntity::getName, stationinfoName);
				List<NrDiggingsEndangerEntity> listEntity2 = nrDiggingsEndangerService.list(queryWrapperDiggings);
				for (NrDiggingsEndangerEntity nrDiggingsEndangerEntity : listEntity2) {
					stationinfoList.add(nrDiggingsEndangerEntity.getId());
				}
				
			}
			
			if(TsBqUtils.isNotNull(stationinfoList)){
				return DataResult.fail("未查询到 "+stationinfoName+" 相关信息！ 行业：" + nrStationinfoForecastMonth.getIndustry());
			}
			
		}else{
			if(TsBqUtils.isNotNull(industry)){

				if(industry.equals("混凝土")||industry.equals("水泥制造")){
					LambdaQueryWrapper<NrConcreteEndangerEntity> queryWrapperConcrete = Wrappers.lambdaQuery();
					
					queryWrapperConcrete.eq(NrConcreteEndangerEntity::getIndustry, industry);
					
					List<NrConcreteEndangerEntity> listEntity = nrConcreteEndangerService.list(queryWrapperConcrete);
					
					for (NrConcreteEndangerEntity nrConcreteEndangerEntity : listEntity) {
						stationinfoList.add(nrConcreteEndangerEntity.getId());
					}
					
				}else if(industry.equals("铁矿选矿")||industry.equals("铁矿采矿")){
					LambdaQueryWrapper<NrDiggingsEndangerEntity> queryWrapperDiggings = Wrappers.lambdaQuery();
					
					queryWrapperDiggings.eq(NrDiggingsEndangerEntity::getIndustry, industry);
					
					List<NrDiggingsEndangerEntity> listEntity = nrDiggingsEndangerService.list(queryWrapperDiggings);
					
					for (NrDiggingsEndangerEntity nrDiggingsEndangerEntity : listEntity) {
						stationinfoList.add(nrDiggingsEndangerEntity.getId());
					}
				}
			}
		}
		
		
		
		
		
		Page page = new Page(nrStationinfoForecastMonth.getPage(), nrStationinfoForecastMonth.getLimit());
		LambdaQueryWrapper<NrStationinfoForecastMonthEntity> queryWrapper = Wrappers.lambdaQuery();
		// 查询条件示例
		
		if(TsBqUtils.isNotNull(nrStationinfoForecastMonth.getTime())){
			queryWrapper.eq(NrStationinfoForecastMonthEntity::getTime, nrStationinfoForecastMonth.getTime());
			
		}
		
		if(TsBqUtils.isNotNull(nrStationinfoForecastMonth.getTaxorgcode())){
			String taxorgcode = nrStationinfoForecastMonth.getTaxorgcode();
			if (Constants.TAX_COUNTES.equals(TsBqUtils.getTaxOrgLevel(taxorgcode))) {
				// 县区局
				queryWrapper.eq(NrStationinfoForecastMonthEntity::getTaxorgcode, taxorgcode);
			} else if (Constants.TAX_CITIES.equals(TsBqUtils.getTaxOrgLevel(taxorgcode))) {
				// 市局
				queryWrapper.like(NrStationinfoForecastMonthEntity::getTaxorgcode, taxorgcode.substring(0, 5));
			} else {
				// 省局
			}
			
		}else{
			return DataResult.success("未选择税务机关");
		}
		
		if(TsBqUtils.isNotNull(stationinfoList)){
			queryWrapper.in(NrStationinfoForecastMonthEntity::getStationinfoId, stationinfoList);
			
		}
		
		IPage<NrStationinfoForecastMonthEntity> iPage = nrStationinfoForecastMonthService.page(page, queryWrapper);
		List<NrStationinfoForecastMonthEntity> records = iPage.getRecords();
		for (NrStationinfoForecastMonthEntity entity : records) {
			String taxorgcode = entity.getTaxorgcode();
			SysDept dept = deptService.getById(taxorgcode);
			if(TsBqUtils.isNotNull(dept)){
				entity.setTaxorgName(dept.getName());
				
			}
			
			String stationinfoId = entity.getStationinfoId();
			GasStationinfoEntity stationinfo = stationinfoService.getById(stationinfoId);
			if(TsBqUtils.isNotNull(stationinfo)){
				entity.setStationinfoName(stationinfo.getName());
				entity.setGasLocation(stationinfo.getGasLocation());
				entity.setGasCoverage(stationinfo.getGasCoverage());
				
				entity.setTaxpayerName(stationinfo.getTaxpayerName());
				entity.setTaxpayerCode(stationinfo.getTaxpayerCode());
			}
		}
		return DataResult.success(iPage);
	}
	
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@ApiOperation(value = "查询分页数据")
	@PostMapping("/list0")
	@ResponseBody
	public DataResult list0(@RequestBody NrStationinfoForecastMonthEntity gasStationinfoForecastMonth) {
		
		String currentDeptId = httpSessionService.getCurrentDeptId();
		String taxorgcode = gasStationinfoForecastMonth.getTaxorgcode();
		
		Page page = new Page(gasStationinfoForecastMonth.getPage(), gasStationinfoForecastMonth.getLimit());
		LambdaQueryWrapper<SysDept> deptQueryWrapper = Wrappers.lambdaQuery();
		// 查询条件示例
		if (TsBqUtils.isNotNull(taxorgcode)) {
			deptQueryWrapper.eq(SysDept::getId, taxorgcode);

		} else {
			if (Constants.TAX_COUNTES.equals(TsBqUtils.getTaxOrgLevel(currentDeptId))) {
				// 县区局
				deptQueryWrapper.eq(SysDept::getId, currentDeptId);
			} else if (Constants.TAX_CITIES.equals(TsBqUtils.getTaxOrgLevel(currentDeptId))) {
				// 市局
				deptQueryWrapper.like(SysDept::getId, currentDeptId.substring(0, 5));
			} else {
				// 省局
			}
		}
		
		IPage<SysDept> iPage = deptService.page(page, deptQueryWrapper);
		List<SysDept> records = iPage.getRecords();
		
		for (SysDept entity : records) {
			String deptId = entity.getId();
			LambdaQueryWrapper<NrStationinfoForecastMonthEntity> queryWrapper = Wrappers.lambdaQuery();
			// 查询条件示例
			
			if (Constants.TAX_COUNTES.equals(TsBqUtils.getTaxOrgLevel(deptId))) {
				// 县区局
				queryWrapper.eq(NrStationinfoForecastMonthEntity::getTaxorgcode, deptId);
			} else if (Constants.TAX_CITIES.equals(TsBqUtils.getTaxOrgLevel(deptId))) {
				// 市局
				queryWrapper.like(NrStationinfoForecastMonthEntity::getTaxorgcode, deptId.substring(0, 5));
			} else {
				// 省局
			}
			
			if(TsBqUtils.isNotNull(gasStationinfoForecastMonth.getTime())){
				queryWrapper.eq(NrStationinfoForecastMonthEntity::getTime, gasStationinfoForecastMonth.getTime());
			}
			
			// 行业
			String industry = gasStationinfoForecastMonth.getIndustry();
			List endangerIdList = new ArrayList<>();
			
			if(TsBqUtils.isNotNull(industry)){
				if(industry.equals("混凝土")||industry.equals("水泥制造")){
					LambdaQueryWrapper<NrConcreteEndangerEntity> queryWrapperConcrete = Wrappers.lambdaQuery();
					
					queryWrapperConcrete.eq(NrConcreteEndangerEntity::getIndustry, industry);
					
					List<NrConcreteEndangerEntity> listEntity = nrConcreteEndangerService.list(queryWrapperConcrete);
					
					for (NrConcreteEndangerEntity nrConcreteEndangerEntity : listEntity) {
						endangerIdList.add(nrConcreteEndangerEntity.getId());
					}
					
				}else if(industry.equals("铁矿选矿")||industry.equals("铁矿采矿")){
					LambdaQueryWrapper<NrDiggingsEndangerEntity> queryWrapperDiggings = Wrappers.lambdaQuery();
					
					queryWrapperDiggings.eq(NrDiggingsEndangerEntity::getIndustry, industry);
					
					List<NrDiggingsEndangerEntity> listEntity = nrDiggingsEndangerService.list(queryWrapperDiggings);
					
					for (NrDiggingsEndangerEntity nrDiggingsEndangerEntity : listEntity) {
						endangerIdList.add(nrDiggingsEndangerEntity.getId());
					}
				}
			}
			
			if(TsBqUtils.isNotNull(endangerIdList)){
				queryWrapper.in(NrStationinfoForecastMonthEntity::getStationinfoId, endangerIdList);

			}
			
			List<NrStationinfoForecastMonthEntity> list = nrStationinfoForecastMonthService.list(queryWrapper);
			BigDecimal forecastValueBd = new BigDecimal(0);
			BigDecimal gatherValueBd = new BigDecimal(0);
			BigDecimal amountDieselBd = new BigDecimal(0);
			BigDecimal amountGasolineBd = new BigDecimal(0);
			
			for (NrStationinfoForecastMonthEntity forecastMonthEntity : list) {
				String forecastValue = forecastMonthEntity.getForecastValue();
				String gatherValue = forecastMonthEntity.getGatherValue();
				String amountDiesel = forecastMonthEntity.getAmountDiesel();
				String amountGasoline = forecastMonthEntity.getAmountGasoline();
				
				if(TsBqUtils.isNotNull(forecastValue)){
					forecastValueBd = forecastValueBd.add(new BigDecimal(forecastValue));
				}
				
				if(TsBqUtils.isNotNull(gatherValue)){
					gatherValueBd = gatherValueBd.add(new BigDecimal(gatherValue));
				}

				if(TsBqUtils.isNotNull(amountDiesel)){
					amountDieselBd = amountDieselBd.add(new BigDecimal(amountDiesel));
				}
				
				if(TsBqUtils.isNotNull(amountGasoline)){
					amountGasolineBd = amountGasolineBd.add(new BigDecimal(amountGasoline));
				}
				
			}
			
			entity.setAmountGasoline(amountGasolineBd.toString());
			entity.setAmountDiesel(amountDieselBd.toString());
			entity.setForecastValue(forecastValueBd.toString());
			entity.setGatherValue(gatherValueBd.toString());
			
			entity.setYearMonths(gasStationinfoForecastMonth.getTime());
			
			
		}
		
		return DataResult.success(iPage);
	}

}
